//
//  ViewController.m
//  WKWebviewDemo
//
//  Created by 许金金 on 17/3/1.
//  Copyright © 2017年 许金金. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>


#define ScreenW self.view.frame.size.width
#define ScreenH self.view.frame.size.height

@interface ViewController ()<WKUIDelegate,WKNavigationDelegate>

@property(nonatomic,strong)WKWebView *webview;

@property(nonatomic,strong) UIProgressView * progressView;

@property (nonatomic,strong) UIActivityIndicatorView *activityIndicatorView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString * urlstr=@"http://10.92.21.54/EMP/empLogin?param=c3lzQ29kZT1CMkM=&isTouch=1&isAPP=Y";
    urlstr=@"https://www.baidu.com";
    self.webview=[[WKWebView alloc] initWithFrame:CGRectMake(0, 20, ScreenW, ScreenH-20)];
    self.webview.navigationDelegate=self;

    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]]];
    [self.navigationController.view addSubview:self.webview];
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
    self.activityIndicatorView.center = self.navigationController.view.center;
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.activityIndicatorView setBackgroundColor:[UIColor lightGrayColor]];
    self.activityIndicatorView.backgroundColor = [UIColor colorWithWhite:0.394 alpha:1.000];
    self.activityIndicatorView.transform = CGAffineTransformMakeScale(2, 2) ;
    [self.navigationController.view addSubview:self.activityIndicatorView];

    
}
#pragma mark- 页面开始加载时调用
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    NSLog(@"页面开始加载了");
   
    [self.activityIndicatorView startAnimating];
    
}
#pragma mark- 当内容开始返回时调用
-(void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
{
    NSLog(@"开始返回内容");
}
#pragma mark- 页面加载完成后调用
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"页面加载完成后调用");
    [self.activityIndicatorView stopAnimating];
}
#pragma mark- 页面加载失败时调用
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"页面加载失败");
        [self.activityIndicatorView stopAnimating];
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"接收到服务器跳转请求");
}
// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    
    NSLog(@"在接收到服务器响应后，决定是否跳转");
//    NSLog(@"%@",navigationResponse.response.URL.absoluteString);
    //允许跳转
//    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}

// 在发送请求之前，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSLog(@"在发送请求之前，决定是否跳转");
//    NSLog(@"%@",navigationAction.request.URL.absoluteString);
    //允许跳转
//    decisionHandler(WKNavigationActionPolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationActionPolicyCancel);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
